﻿using System.Collections.Generic;
using HappyBa.Core.Interfaces;

namespace HappyBa.Core.Validators
{
    public class SingleRoleValidation : IValidator
    {
        public IEnumerable<ValidationResult> Validate(ValidationContext context)
        {
            foreach (var assignable in context.Metrics.Assignables)
            {
                if (context.Metrics.UserRoleAssociations.ContainsKey(assignable.GeneralUser.Id))
                {
                    if (!string.Equals(assignable.Role.Name, context.Metrics.UserRoleAssociations[assignable.GeneralUser.Id]))
                    {
                        yield return new ValidationResult(
                            string.Format("Invalid user '{0}' role '{1}'", assignable.GeneralUser, assignable.Role))
                        {
                            User = assignable.GeneralUser
                        };
                    }
                }
            }
        }
    }
}
